package Model;

import java.util.HashMap;
import java.util.Map;

public class Messages 
{
	
	private final byte[] NOT_EXISTS_PULL = null;
	
	private Map<Integer, byte[]> m_Data;	
	private int m_NextIndex;
	
	public Messages()
	{			
		m_Data = new HashMap<Integer, byte[]>();
		m_NextIndex = 0;
	}
	
	// add data - from message
	public synchronized int append(byte[] msg)
	{		
		int i = m_NextIndex++;
		m_Data.put(i, msg);	
		
		return i;
	}
	
	// add data - from DB
	public void append(byte[] message, int index)
	{		
		m_Data.put(index, message);
		
		if (m_NextIndex <= index)
		{
			m_NextIndex = index + 1;
		}
	}
	
	public byte[] pull(int index)
	{
		if (m_Data.containsKey(index))
		{
			return m_Data.get(index);
		}
		
		return NOT_EXISTS_PULL;
	}
}
